package com.seeyon.apps.taxFund.api;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.seeyon.cap4.form.exception.FormException;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.json.JSONUtil;

import cn.hutool.core.lang.Assert;

import org.springframework.util.Base64Utils;
import www.seeyon.com.utils.Base64Util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: huangjin
 * @time: 2025/9/2 11:23
 */
public class LQAPIRequest {

    public LQAPIRequest(String interfaceCode, Object data) {
        this.global_info = new GLOBAL_INFO();
        this.global_info.setInterface_code(interfaceCode);
        this.global_info.setApplication_code("LQTD");
        this.global_info.setVersion("1.0");

        String appId = AppContext.getSystemProperty("shuijin.api.leQiAppId");
        Assert.notBlank(appId, "乐企appId未配置！");

        String appSecret = AppContext.getSystemProperty("shuijin.api.leQiAppSecret");
        Assert.notBlank(appSecret, "乐企appSecret未配置！");

//        String appId = "qneeyuwbx7l6";
//        String appSecret = "88002e2c2859415cbcfea1771f77e068";
        this.global_info.setApp_id(appId);

        String base64Data;
        if (data instanceof String) {
            base64Data = Base64Util.encode((String) data);
        }else if (data != null) {
            base64Data = Base64Util.encode(JSONUtil.toJSONString(data));
        }else {
            throw new IllegalArgumentException("乐企报文参数异常！");
        }
        this.data = base64Data;
        String str = JSONUtil.toJSONString(this);

        byte[] byteKey = appSecret.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey,"HmacSHA512");
        byte[] byteData = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA512");
            mac.init(secretKeySpec);
            byteData = mac.doFinal(str.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e){
            throw new FormException("乐企接口签名异常！");
        }
        this.data_sign = Base64Utils.encodeToString(byteData);
//        this.data_sign = "11GBGA341K91";
    }

    @JsonProperty(value = "global_info", index = 1)
    private GLOBAL_INFO global_info;
    /**
     * 内层接口报文（Base64编码）
     */
    @JsonProperty(value = "data", index = 2)
    private String data;

    @JsonProperty(value = "data_sign", index = 3)
    private String data_sign = "";

    public String getData_sign() {
        return data_sign;
    }

    public void setData_sign(String data_sign) {
        this.data_sign = data_sign;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public GLOBAL_INFO getGlobal_info() {
        return global_info;
    }

    public void setGlobal_info(GLOBAL_INFO global_info) {
        this.global_info = global_info;
    }

    public static class GLOBAL_INFO {

        @JsonProperty(value = "application_code", index = 1)
        private String application_code;

        @JsonProperty(value = "interface_code", index = 2)
        private String interface_code;

        @JsonProperty(value = "version", index = 3)
        private String version;

        @JsonProperty(value = "app_abbr", index = 4)
        private String app_abbr = "";

        @JsonProperty(value = "app_id", index = 5)
        private String app_id;

        @JsonProperty(value = "data_id", index = 6)
        private String data_id = "";

        public String getApplication_code() {
            return application_code;
        }

        public void setApplication_code(String application_code) {
            this.application_code = application_code;
        }

        public String getInterface_code() {
            return interface_code;
        }

        public void setInterface_code(String interface_code) {
            this.interface_code = interface_code;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getApp_abbr() {
            return app_abbr;
        }

        public void setApp_abbr(String app_abbr) {
            this.app_abbr = app_abbr;
        }

        public String getApp_id() {
            return app_id;
        }

        public void setApp_id(String app_id) {
            this.app_id = app_id;
        }

        public String getData_id() {
            return data_id;
        }

        public void setData_id(String data_id) {
            this.data_id = data_id;
        }
    }

   /* public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("fplx", "01");
        params.put("fpdm", "9999251030");
        params.put("fphm", "00024361");
        params.put("kprq", "20251030");
        params.put("jym", "");
        params.put("kpje", "2602");
        params.put("nsrsbh", "9X000N3HMA21QEYWXL");



        LQAPIRequest lqapiRequest = new LQAPIRequest("BASE.CHECKINVOICE", params);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json;charset=UTF-8");
        String s = restTemplate.postForObject("http://218.92.195.114:8091/stms/openapi/allElectricInfo", new HttpEntity<>(lqapiRequest, httpHeaders), String.class);

        System.out.println(s);
    }*/
}
